package com.china08.yxyapi.api.home.index.notice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNotice;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNoticeRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 首页--焦点图查询
 * 
 */
@CrossOrigin
@RestController
public class IndexSrhNoticeController extends YxyApiBaseController {

    @Autowired
    private YxyHomeNoticeRepository yxyHomeNoticeRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/notice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;

        List<RespModel> respModelLst = new ArrayList<RespModel>();

        if (StringUtils.isNotBlank(schoolId)) //
        {
            // 获取6条数据
            List<YxyHomeNotice> yxyHomeNoticeLst = yxyHomeNoticeRepository
                    .findTop6ByEdbIdAndStatusOrderByCreatedDateDesc(schoolId, 1);

            for (YxyHomeNotice yxyHomeNotice : yxyHomeNoticeLst) //
            {
                RespModel respModel = new RespModel();
                respModel.id = yxyHomeNotice.getId();
                respModel.title = yxyHomeNotice.getTitle();
                respModel.createDate = DateToolUtils.format(yxyHomeNotice.getCreatedDate(), "yyyy-MM-dd");
                respModelLst.add(respModel);
            }
        }
        return respModelLst;
    }

    protected static class RepModel extends YxyApiReqModel {
        public String domain;

        public String schoolId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 公告id
        public String id;
        // 公告标题
        public String title;
        public String createDate;
    }
}
